package com.daxt.service.impl;

import java.nio.charset.Charset;
import java.util.*;
import java.util.regex.Pattern;
import java.util.stream.Collectors;

import cn.hutool.core.codec.Base64Decoder;
import com.daxt.base.common.util.RedisUtil;
import com.daxt.mapper.sys.*;
import com.daxt.model.dic.SystemConfigType;
import com.daxt.model.dic.UserClassifyType;
import com.daxt.model.dic.VersionsMarkType;
import com.daxt.model.dto.Classifys;
import com.daxt.model.service.archives.vo.*;
import com.daxt.model.service.base.result.UserInfo;
import com.daxt.model.service.base.vo.Dept;
import com.daxt.model.service.base.vo.UserClassify;
import com.daxt.model.service.base.vo.UserRole;
import com.daxt.service.UserService;
import com.daxt.utils.BaseUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.SocketUtils;
import org.springframework.util.StringUtils;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.daxt.mapper.archives.ArchivesDocMapper;
import com.daxt.model.service.archives.param.GenCategoryNum;
import com.daxt.model.service.archives.result.ArchivesTree;
import com.daxt.model.service.base.vo.Dic;
import com.daxt.service.FondsListService;

@Service
public class FondsListServiceImpl implements FondsListService {

    @Autowired
    private ArchivesFondsMapper archivesFondsMapper;
    @Autowired
    private ArchivesClassMapper archivesClassMapper;
    @Autowired
    private ArchivesDocMapper archivesDocMapper;
    @Autowired
    private ArchivesTypeMapper archivesTypeMapper;
    @Autowired
    private DicMapper dicMapper;
    @Autowired
    private UserClassifyMapper userClassifyMapper;
    @Autowired
    private UserRoleMapper userRoleMapper;
    @Autowired
    private UserService userService;
    @Autowired
    private DeptMapper deptMapper;

    @Override
    public List<ArchivesTree> getTree(String t) {
        int index = 0;
        QueryWrapper<ArchivesFonds> queryWrapperOrderBy = new QueryWrapper<ArchivesFonds>();
        queryWrapperOrderBy.orderByAsc("sort");
        List<ArchivesFonds> fonds = archivesFondsMapper.selectList(queryWrapperOrderBy);
        //if (fonds != null && fonds.size() > 0) {
        //	fonds = fonds.stream().sorted(Comparator.comparing(ArchivesFonds::getSort)).collect(Collectors.toList());
        //}
        List<ArchivesTree> lists = new ArrayList<ArchivesTree>();
        for (ArchivesFonds f : fonds) {
            ArchivesTree tree = new ArchivesTree();
            tree.setNodeId(f.getId());
            tree.setId((index++) + "");
            tree.setLabel(f.getFondsName());
            tree.setNodeType("0");
            tree.setNodeValue(f.getFondsNum());
            tree.setIsCatalog("1");
            tree.setIndex(f.getSort());
            QueryWrapper<ArchivesType> queryWrapperType = new QueryWrapper<ArchivesType>();
            queryWrapperType.eq("fondsId", f.getId());
            List<ArchivesType> typeList = archivesTypeMapper.selectList(queryWrapperType);
            if (!StringUtils.isEmpty(t)) {
                for (int i = 0; i < typeList.size(); i++) {
                    QueryWrapper<ArchivesDoc> queryWrapper = new QueryWrapper<ArchivesDoc>();
                    queryWrapper.eq("fondsId", f.getId());
                    queryWrapper.eq("typeNum", typeList.get(i).getTypeNum());
                    queryWrapper.eq("fileType", "0".equals(typeList.get(i).getModel()) ? "doc" : "dossier");
                    queryWrapper.eq("state", "1");
                    queryWrapper.select("id");
                    Integer count = archivesDocMapper.selectCount(queryWrapper);
                    typeList.get(i).setTypeName(typeList.get(i).getTypeName() + " (" + count + ")");
                }
            }

            if (typeList != null && typeList.size() > 0) {
                typeList = typeList.stream().sorted(Comparator.comparing(ArchivesType::getSort))
                        .collect(Collectors.toList());
            }
            List<ArchivesTree> typeTree = new ArrayList<ArchivesTree>();
            for (ArchivesType archivesType : typeList) {
                ArchivesTree treeNode = new ArchivesTree();
                treeNode.setNodeId(archivesType.getId());
                treeNode.setId((index++) + "");
                treeNode.setLabel(archivesType.getTypeName());
                treeNode.setNodeType("1");
                treeNode.setNodeValue(archivesType.getTypeNum());
                treeNode.setModel(archivesType.getModel());
                treeNode.setIsCatalog("1");
                treeNode.setIndex(archivesType.getSort());

                List<ArchivesTree> dataTreeList = new ArrayList<ArchivesTree>();

			/*	QueryWrapper<ArchivesClass> queryWrapperClass = new QueryWrapper<ArchivesClass>();
				queryWrapperClass.eq("fondsId", archivesType.getFondsId());
				queryWrapperClass.eq("typeNum", archivesType.getTypeNum());
				List<ArchivesClass> classList = archivesClassMapper.selectList(queryWrapperClass);
			*/
                QueryWrapper<ArchivesClass> queryWrapperClass = new QueryWrapper<ArchivesClass>();
                queryWrapperClass.eq("fondsId", archivesType.getFondsId());
                queryWrapperClass.eq("typeNum", archivesType.getTypeNum());
                queryWrapperClass.eq("parentId", "0");

                List<ArchivesClass> classList = archivesClassMapper.selectList(queryWrapperClass);
                List<ArchivesClass> classTree = getClassTree(classList, archivesType.getFondsId(), archivesType.getTypeNum());
                if (classTree != null && classTree.size() > 0) {
                    classList.addAll(classTree);
                    List<ArchivesClass> classTree1 = getClassTree(classTree, archivesType.getFondsId(), archivesType.getTypeNum());
                    if (classTree1 != null && classTree1.size() > 0) {
                        classList.addAll(classTree1);
                        List<ArchivesClass> classTree2 = getClassTree(classTree1, archivesType.getFondsId(), archivesType.getTypeNum());
                        if (classTree2 != null && classTree2.size() > 0) {
                            classList.addAll(classTree2);
                            List<ArchivesClass> classTree3 = getClassTree(classTree2, archivesType.getFondsId(), archivesType.getTypeNum());
                            if (classTree3 != null && classTree3.size() > 0)
                                classList.addAll(classTree3);
                        }
                    }
                }


                List<ArchivesTree> resultList = new ArrayList<>();
                if (classList != null && classList.size() > 0) {
                    classList = classList.stream().sorted(Comparator.comparing(ArchivesClass::getSort))
                            .collect(Collectors.toList());
                    if (classList != null && classList.size() > 0) {
                        for (int i = 0; i < classList.size(); i++) {
                            ArchivesClass archivesClass = classList.get(i);
                            if (archivesClass.getParentId().intValue() == 0) {
                                ArchivesTree treeClass = new ArchivesTree();
                                if ("1".equals(archivesClass.getClassType())) {
                                    treeClass.setNodeId(archivesClass.getId());
                                    treeClass.setId((index++) + "");
                                    treeClass.setLabel(archivesClass.getClassName());
                                    treeClass.setNodeType("2");
                                    treeClass.setNodeValue(archivesClass.getClassNum());
                                    treeClass.setModel("1");
                                    treeClass.setIsCatalog(archivesClass.getIsCatalog());
                                    treeClass.setIndex(archivesClass.getSort());
                                    treeClass.setParentId(archivesClass.getParentId());
                                    resultList.add(treeClass);
                                } else if ("2".equals(archivesClass.getClassType())) {
                                    if ("archivalYear".equals(archivesClass.getDataColumn())) {
                                        QueryWrapper<ArchivesDoc> queryWrapperYear = new QueryWrapper<ArchivesDoc>();
                                        queryWrapperYear.eq("fondsId", f.getId());
                                        queryWrapperYear.eq("typeNum", archivesType.getTypeNum());
                                        queryWrapperYear.orderByDesc("archivalYear");
                                        queryWrapperYear.groupBy("archivalYear");
                                        queryWrapperYear.select("id", "archivalYear");
                                        List<ArchivesDoc> yearList = archivesDocMapper.selectList(queryWrapperYear);
                                        for (ArchivesDoc archivesDoc : yearList) {
                                            if (!StringUtils.isEmpty(archivesDoc.getArchivalYear())) {
                                                ArchivesTree treeDatac = new ArchivesTree();
                                                treeDatac.setNodeId(archivesClass.getId());
                                                treeDatac.setId((index++) + "");
                                                treeDatac.setLabel(archivesDoc.getArchivalYear());
                                                treeDatac.setNodeType("2");
                                                treeDatac.setNodeValue(archivesDoc.getArchivalYear());
                                                treeDatac.setModel("0");
                                                treeDatac.setColumn(archivesClass.getDataColumn());
                                                treeDatac.setIsCatalog(archivesClass.getIsCatalog());
                                                treeDatac.setIndex(index);
                                                resultList.add(treeDatac);
                                                index++;
                                            }
                                        }
                                    } else {
                                        QueryWrapper<Dic> queryWrapperDicc = new QueryWrapper<Dic>();
                                        queryWrapperDicc.eq("dicType", archivesClass.getDataColumn());
                                        queryWrapperDicc.orderByAsc("sort");

                                        List<Dic> dicListc = dicMapper.selectList(queryWrapperDicc);
                                        for (Dic dicc : dicListc) {
                                            ArchivesTree treeDatac = new ArchivesTree();
                                            treeDatac.setNodeId(archivesClass.getId());
                                            treeDatac.setId((index++) + "");
                                            treeDatac.setLabel(dicc.getDicValue());
                                            treeDatac.setNodeType("2");
                                            treeDatac.setNodeValue(dicc.getDicCode());
                                            treeDatac.setModel("0");
                                            treeDatac.setColumn(archivesClass.getDataColumn());
                                            treeDatac.setIsCatalog(archivesClass.getIsCatalog());
                                            treeDatac.setIndex(index);
                                            resultList.add(treeDatac);
                                            index++;
                                        }
                                    }
                                }
                            }
                        }
                    }
                    setClassChildren(classList, resultList, f.getId(), archivesType.getId()+"", index);
                }
                for (ArchivesTree archivesClass : resultList) {
                    dataTreeList.add(archivesClass);
                }
                treeNode.setChildren(dataTreeList);
                typeTree.add(treeNode);
            }
            tree.setChildren(typeTree);
            lists.add(tree);
        }
        return lists;
    }

//    @Override
//    public String genCategoryNumNew(List<GenCategoryNum> column, String split, Map entity) {
//        String categoryNum = "";
//        if (column == null)
//            return null;
//        if (column.size() > 1 && StringUtils.isEmpty(split)) {
//            return null;
//        } else {
//            for (int i = 0; i < column.size(); i++) {
//                GenCategoryNum genCategoryNum = column.get(i);
//                if (StringUtils.isEmpty(genCategoryNum.getValue())) {
//                    return null;
//                }
//                if (i == 0) {
//                    categoryNum = genCategoryNum.getValue();
//                } else {
//                    categoryNum += split + genCategoryNum.getValue();
//                }
//            }
//        }
//        return categoryNum;
//    }

//    public String genCategoryNum(String fondsId, String typeNum, List<GenCategoryNum> column, String split) {
//        if (column == null || column.size() == 0) {
//            return "";
//        }
//        QueryWrapper<ArchivesFonds> queryWrapperFondesId = new QueryWrapper<ArchivesFonds>();
//        queryWrapperFondesId.eq("fondsId", fondsId);
//        List<ArchivesFonds> fonds = archivesFondsMapper.selectList(queryWrapperFondesId);
//        if (fonds != null && fonds.size() > 0) {
//            fonds = fonds.stream().sorted(Comparator.comparing(ArchivesFonds::getSort)).collect(Collectors.toList());
//        }
//        List<ArchivesTree> lists = new ArrayList<ArchivesTree>();
//        for (ArchivesFonds f : fonds) {
//            ArchivesTree tree = new ArchivesTree();
//            tree.setId(f.getId().toString());
//            tree.setLabel(f.getFondsName());
//            tree.setNodeType("0");
//            tree.setNodeValue(f.getFondsNum());
//            tree.setIsCatalog("1");
//            tree.setColumn("fondsId");
//            tree.setIndex(f.getSort());
//            QueryWrapper<ArchivesType> queryWrapperType = new QueryWrapper<ArchivesType>();
//            queryWrapperType.eq("fondsId", f.getId());
//            queryWrapperType.eq("typeNum", typeNum);
//            List<ArchivesType> typeList = archivesTypeMapper.selectList(queryWrapperType);
//
//            if (typeList != null && typeList.size() > 0) {
//                typeList = typeList.stream().sorted(Comparator.comparing(ArchivesType::getSort))
//                        .collect(Collectors.toList());
//            }
//            List<ArchivesTree> typeTree = new ArrayList<ArchivesTree>();
//            for (ArchivesType archivesType : typeList) {
//                ArchivesTree treeNode = new ArchivesTree();
//                treeNode.setId(archivesType.getId().toString());
//                treeNode.setLabel(archivesType.getTypeName());
//                treeNode.setColumn("typeNum");
//                treeNode.setNodeType("1");
//                treeNode.setNodeValue(archivesType.getTypeNum());
//                treeNode.setModel(archivesType.getModel());
//                treeNode.setIsCatalog("1");
//                treeNode.setIndex(archivesType.getSort());
//
//                List<ArchivesTree> dataTreeList = new ArrayList<ArchivesTree>();
//
//
//                QueryWrapper<ArchivesClass> queryWrapperClass = new QueryWrapper<ArchivesClass>();
//                queryWrapperClass.eq("fondsId", fondsId);
//                queryWrapperClass.eq("typeNum", typeNum);
//                queryWrapperClass.eq("parentId", "0");
//
//                List<ArchivesClass> classList = archivesClassMapper.selectList(queryWrapperClass);
//                List<ArchivesClass> classTree = getClassTree(classList, fondsId, typeNum);
//                if (classTree != null && classTree.size() > 0) {
//                    classList.addAll(classTree);
//                    List<ArchivesClass> classTree1 = getClassTree(classTree, fondsId, typeNum);
//                    if (classTree1 != null && classTree1.size() > 0) {
//                        classList.addAll(classTree1);
//                        List<ArchivesClass> classTree2 = getClassTree(classTree1, fondsId, typeNum);
//                        if (classTree2 != null && classTree2.size() > 0) {
//                            classList.addAll(classTree2);
//                            List<ArchivesClass> classTree3 = getClassTree(classTree2, fondsId, typeNum);
//                            if (classTree3 != null && classTree3.size() > 0)
//                                classList.addAll(classTree3);
//                        }
//                    }
//                }
//
//
//                List<ArchivesTree> resultList = new ArrayList<>();
//                if (classList != null && classList.size() > 0) {
//                    classList = classList.stream().sorted(Comparator.comparing(ArchivesClass::getSort))
//                            .collect(Collectors.toList());
//                    if (classList != null && classList.size() > 0) {
//                        for (int i = 0; i < classList.size(); i++) {
//                            ArchivesClass archivesClass = classList.get(i);
//                            if (archivesClass.getParentId().intValue() == 0) {
//                                ArchivesTree treeClass = new ArchivesTree();
//                                if ("1".equals(archivesClass.getClassType())) {
//                                    treeClass.setId(archivesClass.getId().toString());
//                                    treeClass.setLabel(archivesClass.getClassName());
//                                    treeClass.setNodeType("2");
//                                    treeClass.setColumn("classNum");
//                                    treeClass.setNodeValue(archivesClass.getClassNum());
//                                    treeClass.setModel("1");
//                                    treeClass.setNodeId(archivesClass.getId());
//                                    treeClass.setIsCatalog(archivesClass.getIsCatalog());
//                                    treeClass.setIndex(archivesClass.getSort());
//                                    treeClass.setParentId(archivesClass.getParentId());
//                                    resultList.add(treeClass);
//                                } else if ("2".equals(archivesClass.getClassType())) {
//                                    ArchivesTree treeDatac = new ArchivesTree();
//                                    treeDatac.setId(archivesClass.getId().toString());
//                                    treeDatac.setLabel(archivesClass.getDataColumn());
//                                    treeDatac.setNodeType("2");
//                                    treeDatac.setNodeValue(archivesClass.getDataColumn());
//                                    treeDatac.setModel("0");
//                                    treeDatac.setColumn(archivesClass.getDataColumn());
//                                    treeDatac.setIsCatalog(archivesClass.getIsCatalog());
//                                    treeDatac.setIndex(archivesClass.getSort());
//                                    treeClass.setParentId(archivesClass.getParentId());
//                                    resultList.add(treeDatac);
//                                }
//                            }
//                        }
//                    }
//                    setClassChildren1(classList, resultList, f.getId(), archivesType.getTypeNum());
//                }
//                for (ArchivesTree archivesClass : resultList) {
//                    dataTreeList.add(archivesClass);
//                }
//                treeNode.setChildren(dataTreeList);
//                typeTree.add(treeNode);
//            }
//            tree.setChildren(typeTree);
//            lists.add(tree);
//        }
//        List<String> result = new ArrayList<String>();
//        getcategoryNum(lists, column, result);
//        String categoryNum = "";
//        if (result != null && result.size() > 0)
//            for (int i = 0; i < result.size(); i++) {
//                if (result.get(i).indexOf(":") > 0) {
//                    if (i == 0)
//                        categoryNum += result.get(i).split(":")[1];
//                    else
//                        categoryNum += split + result.get(i).split(":")[1];
//                }
//            }
//        else {
//            return categoryNum;
//        }
//        return categoryNum;
//    }

    private void getcategoryNum(List<ArchivesTree> list, List<GenCategoryNum> column, List<String> result) {

        for (ArchivesTree tree : list) {

            if ("0".equals(tree.getNodeType())) {
                if (tree.getChildren() != null && tree.getChildren().size() > 0) {
                    getcategoryNum(tree.getChildren(), column, result);
                }
            } else if ("1".equals(tree.getNodeType())) {
                for (GenCategoryNum genCategoryNum : column) {
                    if (tree.getColumn().equals(genCategoryNum.getColumn())) {
                        result.add(genCategoryNum.getColumn() + ":" + tree.getIndex() + "");
                        if (tree.getChildren() != null && tree.getChildren().size() > 0) {
                            getcategoryNum(tree.getChildren(), column, result);
                        }
                    }
                }
            } else if ("2".equals(tree.getNodeType())) {
                if ("0".equals(tree.getModel())) {
                    if ("0".equals(tree.getIsCatalog())) {
                        int i = 0;
                        for (GenCategoryNum genCategoryNum : column) {
                            if (i > 0)
                                continue;
                            if (tree.getColumn().equals(genCategoryNum.getColumn())) {
                                result.add(genCategoryNum.getColumn() + ":" + genCategoryNum.getValue());
                                i++;
                                if (tree.getChildren() != null && tree.getChildren().size() > 0) {
                                    getcategoryNum(tree.getChildren(), column, result);
                                }
                            }
                        }
                    } else if ("1".equals(tree.getIsCatalog())) {
                        int i = 0;
                        for (GenCategoryNum genCategoryNum : column) {
                            if (i > 0)
                                continue;
                            if (tree.getColumn().equals(genCategoryNum.getColumn()) && genCategoryNum.getId().intValue() == tree.getNodeId().intValue()) {
                                result.add(genCategoryNum.getColumn() + ":" + tree.getIndex() + "");
                                i++;
                                if (tree.getChildren() != null && tree.getChildren().size() > 0) {
                                    getcategoryNum(tree.getChildren(), column, result);
                                }
                            }
                        }
                    }
                } else if ("1".equals(tree.getModel())) {
                    int i = 0;
                    for (GenCategoryNum genCategoryNum : column) {
                        if (i > 0)
                            continue;
                        if ("classNum".equals(tree.getColumn())
                                && tree.getColumn().equals(genCategoryNum.getColumn())) {
                            if (tree.getNodeValue().equals(genCategoryNum.getValue())) {
                                {
                                    result.add(genCategoryNum.getColumn() + ":" + tree.getIndex() + "");
                                    i++;
                                }
                                if (tree.getChildren() != null && tree.getChildren().size() > 0) {
                                    getcategoryNum(tree.getChildren(), column, result);
                                }
                            }
                        } else if (tree.getColumn().equals(genCategoryNum.getColumn())) {
                            if (i > 0)
                                continue;
                            result.add(genCategoryNum.getColumn() + ":" + tree.getIndex() + "");
                            i++;
                            if (tree.getChildren() != null && tree.getChildren().size() > 0) {
                                getcategoryNum(tree.getChildren(), column, result);
                            }
                        }
                    }
                }
            }
        }

    }

    private void setClassChildren(List<ArchivesClass> list, List<ArchivesTree> resultList, long fondsId,
                                  String typeId, int index) {
        for (int i = 0; i < resultList.size(); i++) {
            ArchivesTree archivesClass = resultList.get(i);
            for (int j = 0; j < list.size(); j++) {
                ArchivesClass archivesClassTemp = list.get(j);
                if (archivesClass.getNodeId().intValue() == archivesClassTemp.getParentId().intValue()) {
                    List<ArchivesTree> children = archivesClass.getChildren();
                    ArchivesTree treeClass = new ArchivesTree();
                    if ("1".equals(archivesClassTemp.getClassType())) {
                        treeClass.setNodeId(archivesClassTemp.getId());
                        treeClass.setId(fondsId + typeId + archivesClassTemp.getId() + archivesClassTemp.getSort() + index);
                        treeClass.setLabel(archivesClassTemp.getClassName());
                        treeClass.setNodeType("2");
                        treeClass.setNodeValue(archivesClassTemp.getClassNum());
                        treeClass.setModel("1");
                        treeClass.setColumn("classNum");
                        treeClass.setIsCatalog(archivesClassTemp.getIsCatalog());
                        treeClass.setIndex(archivesClassTemp.getSort());
                        treeClass.setParentId(archivesClass.getParentId());
                        treeClass.setTemplate(archivesClassTemp.getTemplate());
                        children.add(treeClass);
                        index++;
                    } else if ("2".equals(archivesClassTemp.getClassType())) {
                        if ("archivalYear".equals(archivesClassTemp.getDataColumn())) {
                            QueryWrapper<ArchivesDoc> queryWrapperYear = new QueryWrapper<ArchivesDoc>();
                            queryWrapperYear.eq("fondsId", fondsId);
                            queryWrapperYear.eq("typeId", typeId);
                            queryWrapperYear.orderByDesc("archivalYear");
                            queryWrapperYear.groupBy("archivalYear");
                            queryWrapperYear.select("id", "archivalYear");
                            List<ArchivesDoc> yearList = archivesDocMapper.selectList(queryWrapperYear);
                            for (ArchivesDoc archivesDoc : yearList) {
                                if (!StringUtils.isEmpty(archivesDoc.getArchivalYear())) {
                                    ArchivesTree treeDatac = new ArchivesTree();
                                    treeDatac.setId(fondsId + typeId + archivesClassTemp.getId()
                                            + archivesClassTemp.getSort() + archivesDoc.getArchivalYear() + index);
                                    //lig 2021-06-23 临时修改解决字段下分类不显示问题
//									treeDatac.setNodeId(archivesDoc.getId());
                                    treeDatac.setNodeId(archivesClassTemp.getId());
                                    treeDatac.setLabel(archivesDoc.getArchivalYear());
                                    treeDatac.setNodeType("2");
                                    treeDatac.setNodeValue(archivesDoc.getArchivalYear());
                                    treeDatac.setModel("0");
                                    treeDatac.setColumn(archivesClassTemp.getDataColumn());
                                    treeDatac.setIsCatalog(archivesClassTemp.getIsCatalog());
                                    treeDatac.setIndex(archivesClassTemp.getSort());
                                    treeDatac.setTemplate(archivesClassTemp.getTemplate());
                                    children.add(treeDatac);
                                    index++;
                                }
                            }
                        } else {
                            QueryWrapper<Dic> queryWrapperDicc = new QueryWrapper<Dic>();
                            queryWrapperDicc.eq("dicType", archivesClassTemp.getDataColumn());
                            queryWrapperDicc.orderByAsc("sort");

                            List<Dic> dicListc = dicMapper.selectList(queryWrapperDicc);
                            //int indexc = 1;
                            for (Dic dicc : dicListc) {
                                ArchivesTree treeDatac = new ArchivesTree();
                                treeDatac.setId(fondsId + typeId + archivesClassTemp.getId()
                                        + archivesClassTemp.getSort() + dicc.getId() + index);
                                treeDatac.setNodeId(dicc.getId());
                                treeDatac.setLabel(dicc.getDicValue());
                                treeDatac.setNodeType("2");
                                treeDatac.setNodeValue(dicc.getDicCode());
                                treeDatac.setModel("0");
                                treeDatac.setColumn(archivesClassTemp.getDataColumn());
                                treeDatac.setIsCatalog(archivesClassTemp.getIsCatalog());
                                treeDatac.setIndex(archivesClassTemp.getSort());
                                children.add(treeDatac);
                                index++;
                            }
                        }

                    }
                    archivesClass.setChildren(children);
                }
            }
            List<ArchivesTree> children = archivesClass.getChildren();
            setClassChildren(list, children, fondsId, typeId, index);
            // archivesClass.setChildren(children);
            resultList.set(i, archivesClass);
        }
    }


    /**
     * @description: 增加数据权限
     * @author: lig
     * @date: 2021/6/29
     */
    private void setClassChildren(List<ArchivesClass> list, List<ArchivesTree> resultList, String fondsId,
                                  String typeId, int index, List<String> fieldYearIds) {
        for (int i = 0; i < resultList.size(); i++) {
            ArchivesTree archivesClass = resultList.get(i);
            for (int j = 0; j < list.size(); j++) {
                ArchivesClass archivesClassTemp = list.get(j);
                if (archivesClass.getNodeId().intValue() == archivesClassTemp.getParentId().intValue()) {
                    List<ArchivesTree> children = archivesClass.getChildren();
                    ArchivesTree treeClass = new ArchivesTree();
                    if ("1".equals(archivesClassTemp.getClassType())) {
                        treeClass.setNodeId(archivesClassTemp.getId());
                        treeClass.setId(fondsId + typeId + archivesClassTemp.getId() + archivesClassTemp.getSort() + index);
                        treeClass.setLabel(archivesClassTemp.getClassName());
                        treeClass.setNodeType("2");
                        treeClass.setNodeValue(archivesClassTemp.getClassNum());
                        treeClass.setModel("1");
                        treeClass.setColumn("classNum");
                        treeClass.setIsCatalog(archivesClassTemp.getIsCatalog());
                        treeClass.setIndex(archivesClassTemp.getSort());
                        treeClass.setParentId(archivesClass.getParentId());
                        treeClass.setTemplate(archivesClassTemp.getTemplate());
                        treeClass.setClassNum(archivesClassTemp.getTreeStr());
                        children.add(treeClass);
                        index++;
                    } else if ("2".equals(archivesClassTemp.getClassType())) {
                        if ("archivalYear".equals(archivesClassTemp.getDataColumn())) {
                            QueryWrapper<ArchivesDoc> queryWrapperYear = new QueryWrapper<ArchivesDoc>();
                            queryWrapperYear.eq("fondsId", fondsId);
                            queryWrapperYear.eq("typeId", typeId);
                            //年度字段增加数据权限  lig  2021-06-28
                            if (BaseUtil.listIsNotNullElement(fieldYearIds))
                                queryWrapperYear.in("archivalYear", fieldYearIds);
                            queryWrapperYear.orderByDesc("archivalYear");
                            queryWrapperYear.groupBy("archivalYear");
                            queryWrapperYear.select("id", "archivalYear");
                            List<ArchivesDoc> yearList = archivesDocMapper.selectList(queryWrapperYear);
                            for (ArchivesDoc archivesDoc : yearList) {
                                if (!StringUtils.isEmpty(archivesDoc.getArchivalYear())) {
                                    ArchivesTree treeDatac = new ArchivesTree();
                                    treeDatac.setId(fondsId + typeId + archivesClassTemp.getId()
                                            + archivesClassTemp.getSort() + archivesDoc.getArchivalYear() + index);
                                    //lig 2021-06-23 临时修改解决字段下分类不显示问题
//									treeDatac.setNodeId(archivesDoc.getId());
                                    treeDatac.setNodeId(archivesClassTemp.getId());
                                    treeDatac.setLabel(archivesDoc.getArchivalYear());
                                    treeDatac.setNodeType("2");
                                    treeDatac.setNodeValue(archivesDoc.getArchivalYear());
                                    treeDatac.setModel("0");
                                    treeDatac.setColumn(archivesClassTemp.getDataColumn());
                                    treeDatac.setIsCatalog(archivesClassTemp.getIsCatalog());
                                    treeDatac.setIndex(archivesClassTemp.getSort());
                                    treeDatac.setTemplate(archivesClassTemp.getTemplate());
                                    children.add(treeDatac);
                                    index++;
                                }
                            }
                        } else {
                            QueryWrapper<Dic> queryWrapperDicc = new QueryWrapper<Dic>();
                            queryWrapperDicc.eq("dicType", archivesClassTemp.getDataColumn());
                            queryWrapperDicc.orderByAsc("sort");

                            List<Dic> dicListc = dicMapper.selectList(queryWrapperDicc);
                            //int indexc = 1;
                            for (Dic dicc : dicListc) {
                                ArchivesTree treeDatac = new ArchivesTree();
                                treeDatac.setId(fondsId + typeId + archivesClassTemp.getId()
                                        + archivesClassTemp.getSort() + dicc.getId() + index);
                                treeDatac.setNodeId(dicc.getId());
                                treeDatac.setLabel(dicc.getDicValue());
                                treeDatac.setNodeType("2");
                                treeDatac.setNodeValue(dicc.getDicCode());
                                treeDatac.setModel("0");
                                treeDatac.setColumn(archivesClassTemp.getDataColumn());
                                treeDatac.setIsCatalog(archivesClassTemp.getIsCatalog());
                                treeDatac.setIndex(archivesClassTemp.getSort());
                                children.add(treeDatac);
                                index++;
                            }
                        }

                    }
                    archivesClass.setChildren(children);
                }
            }
            List<ArchivesTree> children = archivesClass.getChildren();
            setClassChildren(list, children, Long.parseLong(fondsId), typeId, index);
            // archivesClass.setChildren(children);
            resultList.set(i, archivesClass);
        }
    }

    private void setClassChildren1(List<ArchivesClass> list, List<ArchivesTree> resultList, long fondsId,
                                   String typeNum) {
        for (int i = 0; i < resultList.size(); i++) {
            ArchivesTree archivesClass = resultList.get(i);
            for (int j = 0; j < list.size(); j++) {
                ArchivesClass archivesClassTemp = list.get(j);
                if (archivesClass.getId().equals(archivesClassTemp.getParentId().toString())) {
                    List<ArchivesTree> children = archivesClass.getChildren();
                    ArchivesTree treeClass = new ArchivesTree();
                    if ("1".equals(archivesClassTemp.getClassType())) {
                        treeClass.setId(archivesClassTemp.getId().toString());
                        treeClass.setLabel(archivesClassTemp.getClassName());
                        treeClass.setNodeType("2");
                        treeClass.setNodeValue(archivesClassTemp.getClassNum());
                        treeClass.setModel("1");
                        treeClass.setColumn("classNum");
                        treeClass.setIsCatalog(archivesClassTemp.getIsCatalog());
                        treeClass.setIndex(archivesClassTemp.getSort());
                        treeClass.setParentId(archivesClass.getParentId());
                        treeClass.setTemplate(archivesClass.getTemplate());
                        children.add(treeClass);
                    } else if ("2".equals(archivesClassTemp.getClassType())) {
                        ArchivesTree treeDatac = new ArchivesTree();
                        treeDatac.setId(archivesClassTemp.getId().toString());
                        treeDatac.setLabel(archivesClassTemp.getDataColumn());
                        treeDatac.setNodeType("2");
                        treeDatac.setNodeValue(archivesClassTemp.getDataColumn());
                        treeDatac.setModel("0");
                        treeDatac.setColumn(archivesClassTemp.getDataColumn());
                        treeDatac.setIsCatalog(archivesClassTemp.getIsCatalog());
                        treeDatac.setIndex(archivesClassTemp.getSort());
                        treeClass.setParentId(archivesClass.getParentId());
                        children.add(treeDatac);

                    }
                    archivesClass.setChildren(children);
                }
            }
            List<ArchivesTree> children = archivesClass.getChildren();
            setClassChildren1(list, children, fondsId, typeNum);
            resultList.set(i, archivesClass);
        }
    }

    @Override
    public String genArchivalNum(String genArchivalNum, String genType, Map entity) {

        String[] split = null;
        if (!StringUtils.isEmpty(genType)) {
            String regex = "[" + Pattern.quote(genType) + "]";
            split =    genArchivalNum.split(regex);
//            split = genArchivalNum.split(genType);
        }
        String[] split1 = genType.split(",");
        System.out.println("split1:"+split1);
        String genNum = "";
        if (split != null) {
            for (int i = 0; i < split.length; i++) {
                Object object = entity.get(split[i]);

                String str = object == null ? "" : object.toString();
                if (RedisUtil.isAppoint(VersionsMarkType.VERSIONS_2)) {
                    if (split[i].equals("classNum")) str = str.replace("·","");
                }
                if (i == 0) {
                    genNum = str;
                } else {
                    genNum += split1[i-1] + str;
                }
            }
        }
        return genNum;
    }

//    @Override
//    public List<GenCategoryNum> genCategoryList(String genCategoryNum, String genCategoryType, Map entity) {
//        if (StringUtils.isEmpty(genCategoryNum) || StringUtils.isEmpty(genCategoryType)) {
//            return null;
//        }
//        String[] splitClass = genCategoryNum.split(genCategoryType);
//        List<GenCategoryNum> column = new ArrayList<GenCategoryNum>();
//        if (splitClass != null) {
//            for (int i = 0; i < splitClass.length; i++) {
//                Object object = entity.get(splitClass[i]);
//                if (object != null) {
//                    if ("classNum".equals(splitClass[i]) && object.toString().contains("/")) {
//                        String[] split = object.toString().split("/");
//                        if (split != null && split.length > 0) {
//                            for (String string : split) {
//                                GenCategoryNum genCategoryNumEntity = new GenCategoryNum();
//                                // genCategoryNumEntity.setId(id);
//                                genCategoryNumEntity.setColumn(splitClass[i]);
//                                genCategoryNumEntity.setValue(string);
//                                column.add(genCategoryNumEntity);
//                            }
//                        }
//                    } else {
//                        GenCategoryNum genCategoryNumEntity = new GenCategoryNum();
//                        // genCategoryNumEntity.setId(id);
//                        genCategoryNumEntity.setColumn(splitClass[i]);
//                        genCategoryNumEntity.setValue(object == null ? "" : object.toString());
//                        column.add(genCategoryNumEntity);
//                    }
//                }
//            }
//        }
//        return column;
//    }

    /**
     * @description: 改造树查询
     * @author: lig
     * @date: 2021/7/20
     */
    @Override
    public List<ArchivesTree> getFondsTree(String t, UserInfo loginInfo,String model,String fondsId) {

        //获取用户数据权限
        Classifys c = userService.buildUserClassifys(loginInfo);
        if (null == c) {
            //获取部门权限
            c = userService.buildUserDeptPermissions(loginInfo);
        }
        List<Long> afIds = c.getFonds();//全宗ids
        List<Long> categoryIds = c.getCategoryIds();//门类ids
        List<Long> classIds = c.getClassIds();//类目ids
        List<String> fieldYearIds = c.getFieldYearIds();//字段年度ids

        //根据部门所属全宗id，获取对应数据
        String role= loginInfo.getRole();
        String deptId = loginInfo.getDeptId();
        List<Dept> deptList=null;
        List<Long> parentDeptFondsIds=new ArrayList<Long>();
        if(!StringUtils.isEmpty(deptId)){
            deptList=deptMapper.getAllParentDept(Long.parseLong(deptId));
            for (int i=0;i<deptList.size();i++){
                parentDeptFondsIds.add(deptList.get(i).getFondsId());
            }
        }
        //if(!"超级管理员".equals(role)  && deptList!=null){   年后确认role的值是超级管理员还是admin
        if("admin".equals(loginInfo.getUsername()) || "超级管理员".equals(role) ){
            fondsId=null;
        }


        QueryWrapper<ArchivesFonds> queryWrapperOrderBy = new QueryWrapper<ArchivesFonds>();
        if(!"admin".equals(loginInfo.getUsername()) &&BaseUtil.listIsNotNullElement(afIds))queryWrapperOrderBy.in("id", afIds);
        queryWrapperOrderBy.eq(!StringUtils.isEmpty(fondsId),"id", fondsId);
        queryWrapperOrderBy.orderByAsc("sort");
        List<ArchivesFonds> fonds = archivesFondsMapper.selectList(queryWrapperOrderBy);

        List<ArchivesTree> lists = new ArrayList<ArchivesTree>();
        for (ArchivesFonds f : fonds) {
            ArchivesTree tree = new ArchivesTree();
            tree.setNodeId(f.getId());
            tree.setId(f.getId() + "");
            tree.setLabel(f.getFondsName());
            tree.setNodeType("0");
            tree.setNodeValue(f.getFondsNum());
            tree.setIsCatalog("1");
            tree.setIndex(f.getSort());


            QueryWrapper<ArchivesType> queryWrapperType = new QueryWrapper<ArchivesType>();
            queryWrapperType.eq("fondsId", f.getId());
            queryWrapperType.eq(!StringUtils.isEmpty(model),"model", model);
            if (BaseUtil.listIsNotNullElement(categoryIds)) queryWrapperType.in("id", categoryIds);

            List<ArchivesType> typeList = archivesTypeMapper.selectList(queryWrapperType);
            List<ArchivesTree> typeTree = new ArrayList<ArchivesTree>();

            for (ArchivesType typeBean : typeList) {
                ArchivesTree treeNode = buildArchivesType(typeBean, f);
                buildArchivesClass(typeBean, f, classIds, fieldYearIds, treeNode, typeTree);
            }

            if (typeTree != null && typeTree.size() > 0) {
                typeTree = typeTree.stream().sorted(Comparator.comparing(ArchivesTree::getIndex))
                        .collect(Collectors.toList());
            }
            tree.setChildren(typeTree);
            if (tree != null)
                lists.add(tree);
        }
        return lists;
    }

    @Override
    public List<Map> fondsTypeExamineCount(String t, UserInfo loginInfo, String fondsId) {

        List<Map> list = new ArrayList<Map>();

        //获取用户数据权限
        Classifys c = userService.buildUserClassifys(loginInfo);
        List<Long> afIds = c.getFonds();//全宗ids
        List<Long> categoryIds = c.getCategoryIds();//门类ids
        List<Long> classIds = c.getClassIds();//类目ids
        List<String> fieldYearIds = c.getFieldYearIds();//字段年度ids


        QueryWrapper<ArchivesFonds> queryWrapperOrderBy = new QueryWrapper<ArchivesFonds>();
        if (BaseUtil.listIsNotNullElement(afIds)) queryWrapperOrderBy.in("id", afIds);
        queryWrapperOrderBy.eq("id", fondsId);
        queryWrapperOrderBy.orderByAsc("sort");
        ArchivesFonds fonds = archivesFondsMapper.selectOne(queryWrapperOrderBy);

        if (null == fonds) return list;

        List<ArchivesTree> lists = new ArrayList<ArchivesTree>();

        QueryWrapper<ArchivesType> queryWrapperType = new QueryWrapper<ArchivesType>();
        queryWrapperType.eq("fondsId", fonds.getId());
        if (BaseUtil.listIsNotNullElement(categoryIds)) queryWrapperType.in("id", categoryIds);
        List<ArchivesType> typeList = archivesTypeMapper.selectList(queryWrapperType);


        for (int i = 0; i < typeList.size(); i++) {
            QueryWrapper<ArchivesDoc> queryWrapper = new QueryWrapper<ArchivesDoc>();
            queryWrapper.eq("fondsId", fonds.getId());
            queryWrapper.eq("typeNum", typeList.get(i).getTypeNum());
            if (!StringUtils.isEmpty(t) && "1".equals(t)) {
                queryWrapper.eq("state", "1");
                queryWrapper.eq("fileType", "0".equals(typeList.get(i).getModel()) ? "doc" : "dossier");
                queryWrapper.eq("isDelete", "0");
            }
            if (!StringUtils.isEmpty(t) && "2".equals(t)) {
                queryWrapper.eq("isDelete", "1");
            }
            queryWrapper.select("id");
            Integer count = archivesDocMapper.selectCount(queryWrapper);
            Map m = new HashMap();
            m.put("fondsId", fonds.getId());
            m.put("typeNum", typeList.get(i).getTypeNum());
            m.put("count", count);
            list.add(m);
        }


        return list;
    }

    @Override
    public List<ArchivesTree> getFondsTree(String t) {

//		QueryWrapper<UserClassify> ucQw=new QueryWrapper<UserClassify>();
//		ucQw.eq()
//		queryWrapperOrderBy.orderByAsc("sort");
//		List<ArchivesFonds> fonds = archivesFondsMapper.selectList(queryWrapperOrderBy);


        QueryWrapper<ArchivesFonds> queryWrapperOrderBy = new QueryWrapper<ArchivesFonds>();
        queryWrapperOrderBy.orderByAsc("sort");
        List<ArchivesFonds> fonds = archivesFondsMapper.selectList(queryWrapperOrderBy);
        //if (fonds != null && fonds.size() > 0) {
        //	fonds = fonds.stream().sorted(Comparator.comparing(ArchivesFonds::getSort)).collect(Collectors.toList());
        //}

        List<ArchivesTree> lists = new ArrayList<ArchivesTree>();
        for (ArchivesFonds f : fonds) {
            ArchivesTree tree = new ArchivesTree();
            tree.setNodeId(f.getId());
            tree.setId(f.getSort() + "");
            tree.setLabel(f.getFondsName());
            tree.setNodeType("0");
            tree.setNodeValue(f.getFondsNum());
            tree.setIsCatalog("1");
            tree.setIndex(f.getSort());
            QueryWrapper<ArchivesType> queryWrapperType = new QueryWrapper<ArchivesType>();
            queryWrapperType.eq("fondsId", f.getId());
            List<ArchivesType> typeList = archivesTypeMapper.selectList(queryWrapperType);
            if (!StringUtils.isEmpty(t) && "1".equals(t)) {
                for (int i = 0; i < typeList.size(); i++) {
                    QueryWrapper<ArchivesDoc> queryWrapper = new QueryWrapper<ArchivesDoc>();
                    queryWrapper.eq("fondsId", f.getId());
                    queryWrapper.eq("typeId", typeList.get(i).getId());
                    queryWrapper.eq("fileType", "0".equals(typeList.get(i).getModel()) ? "doc" : "dossier");
                    queryWrapper.eq("state", "1");
                    queryWrapper.eq("isDelete", "0");
//					queryWrapper.notIn("")
                    queryWrapper.select("id");
                    Integer count = archivesDocMapper.selectCount(queryWrapper);
                    typeList.get(i).setTypeName(typeList.get(i).getTypeName() + " (" + count + ")");
                }
            } else if (!StringUtils.isEmpty(t) && "2".equals(t)) {
                for (int i = 0; i < typeList.size(); i++) {
                    QueryWrapper<ArchivesDoc> queryWrapper = new QueryWrapper<ArchivesDoc>();
                    queryWrapper.eq("fondsId", f.getId());
                    queryWrapper.eq("typeId", typeList.get(i).getId());
                    queryWrapper.eq("isDelete", "1");
                    queryWrapper.select("id");
                    Integer count = archivesDocMapper.selectCount(queryWrapper);
                    typeList.get(i).setTypeName(typeList.get(i).getTypeName() + " (" + count + ")");
                }
            }

            List<ArchivesTree> typeTree = new ArrayList<ArchivesTree>();
            for (ArchivesType archivesType : typeList) {
                ArchivesTree treeNode = new ArchivesTree();
                treeNode.setNodeId(archivesType.getId());
                treeNode.setId(f.getSort() + "-" + archivesType.getSort());
                treeNode.setLabel(archivesType.getTypeName());
                treeNode.setNodeType("1");
                treeNode.setNodeValue(archivesType.getTypeNum());
                treeNode.setModel(archivesType.getModel());
                treeNode.setIsCatalog("1");
                treeNode.setFileType(archivesType.getFileType());
                treeNode.setIndex(archivesType.getSort());

                QueryWrapper<ArchivesClass> queryWrapperClass = new QueryWrapper<ArchivesClass>();
                queryWrapperClass.eq("fondsId", archivesType.getFondsId());
                queryWrapperClass.eq("typeId", archivesType.getId());
                queryWrapperClass.eq("parentId", "0");
                List<ArchivesClass> classList = archivesClassMapper.selectList(queryWrapperClass);

                List<ArchivesTree> resultList = new ArrayList<>();

                /*
                 * classList =
                 * classList.stream().sorted(Comparator.comparing(ArchivesClass::getSort))
                 * .collect(Collectors.toList());
                 */
                if (classList != null && classList.size() > 0) {
                    for (ArchivesClass archivesClassTemp : classList) {
                        ArchivesClass archivesClass = archivesClassTemp;
                        if (archivesClass.getParentId().intValue() == 0) {
                            ArchivesTree treeClass = new ArchivesTree();
                            if ("1".equals(archivesClass.getClassType())) {
                                treeClass.setNodeId(archivesClass.getId());
                                treeClass.setId(f.getSort() + "" + archivesType.getSort() + archivesClass.getIsCatalog()
                                        + archivesClass.getSort());
                                treeClass.setLabel(archivesClass.getClassName());
                                treeClass.setNodeType("2");
                                treeClass.setNodeValue(archivesClass.getClassNum());
                                treeClass.setModel("1");
                                treeClass.setIsCatalog(archivesClass.getIsCatalog());
                                treeClass.setIndex(archivesClass.getSort());
                                treeClass.setParentId(archivesClass.getParentId());
                                resultList.add(treeClass);
                            } else if ("2".equals(archivesClass.getClassType())) {
                                if ("archivalYear".equals(archivesClass.getDataColumn())) {
                                    QueryWrapper<ArchivesDoc> queryWrapperYear = new QueryWrapper<ArchivesDoc>();
                                    queryWrapperYear.eq("fondsId", f.getId());
                                    queryWrapperYear.eq("typeId", archivesType.getId());
                                    queryWrapperYear.orderByDesc("archivalYear");
                                    queryWrapperYear.groupBy("archivalYear");
                                    queryWrapperYear.select("id", "archivalYear");
                                    List<ArchivesDoc> yearList = archivesDocMapper.selectList(queryWrapperYear);
                                    int indexYear = 1;
                                    for (ArchivesDoc archivesDoc : yearList) {
                                        if (!StringUtils.isEmpty(archivesDoc.getArchivalYear())) {
                                            ArchivesTree treeDatac = new ArchivesTree();
                                            treeDatac.setNodeId(archivesClass.getId());
                                            treeDatac.setId(f.getSort() + "" + archivesType.getSort()
                                                    + archivesClass.getIsCatalog() + archivesDoc.getArchivalYear());
                                            treeDatac.setLabel(archivesDoc.getArchivalYear());
                                            treeDatac.setNodeType("2");
                                            treeDatac.setNodeValue(archivesDoc.getArchivalYear());
                                            treeDatac.setModel("0");
                                            treeDatac.setColumn(archivesClass.getDataColumn());
                                            treeDatac.setIsCatalog(archivesClass.getIsCatalog());
                                            treeDatac.setIndex(indexYear);
                                            resultList.add(treeDatac);
                                            indexYear++;
                                        }
                                    }
                                } else {
                                    QueryWrapper<Dic> queryWrapperDicc = new QueryWrapper<Dic>();
                                    queryWrapperDicc.eq("dicType", archivesClass.getDataColumn());
                                    queryWrapperDicc.orderByAsc("sort");

                                    List<Dic> dicListc = dicMapper.selectList(queryWrapperDicc);
                                    for (Dic dicc : dicListc) {
                                        ArchivesTree treeDatac = new ArchivesTree();
                                        treeDatac.setNodeId(archivesClass.getId());
                                        treeDatac.setId(f.getSort() + "" + archivesType.getSort()
                                                + archivesClass.getIsCatalog() + dicc.getSort() + dicc.getId());
                                        treeDatac.setLabel(dicc.getDicValue());
                                        treeDatac.setNodeType("2");
                                        treeDatac.setNodeValue(dicc.getDicCode());
                                        treeDatac.setModel("0");
                                        treeDatac.setColumn(archivesClass.getDataColumn());
                                        treeDatac.setIsCatalog(archivesClass.getIsCatalog());
                                        treeDatac.setIndex(dicc.getSort());
                                        resultList.add(treeDatac);
                                    }
                                }
                            }
                        }
                    }
                }
                // setClassChildren(classList,
                // resultList,f.getId(),archivesType.getTypeNum(),index);
                List<ArchivesTree> resultClassTree = resultList;
                if (resultClassTree != null && resultClassTree.size() > 0) {
                    resultClassTree = resultClassTree.stream().sorted(Comparator.comparing(ArchivesTree::getIndex))
                            .collect(Collectors.toList());
                }

                treeNode.setChildren(resultClassTree);
                typeTree.add(treeNode);
            }
            if (typeTree != null && typeTree.size() > 0) {
                typeTree = typeTree.stream().sorted(Comparator.comparing(ArchivesTree::getIndex))
                        .collect(Collectors.toList());
            }
            tree.setChildren(typeTree);
            if (tree != null)
                lists.add(tree);
        }
        return lists;
    }

    private List<ArchivesClass> getClassTree(List<ArchivesClass> parentIds, String fondsId, String typeId) {
        if (parentIds != null && parentIds.size() > 0) {
            List<Long> ids = new ArrayList<Long>();
            for (ArchivesClass archivesClass : parentIds) {
                ids.add(archivesClass.getId());
            }
            QueryWrapper<ArchivesClass> queryWrapperClass = new QueryWrapper<ArchivesClass>();
            queryWrapperClass.eq("fondsId", fondsId);
            queryWrapperClass.eq("typeId", typeId);
            queryWrapperClass.in("parentId", ids);

            List<ArchivesClass> classList = archivesClassMapper.selectList(queryWrapperClass);
            if (classList != null && classList.size() > 0) {
                classList = classList.stream().sorted(Comparator.comparing(ArchivesClass::getSort))
                        .collect(Collectors.toList());
            }
            return classList;
        } else {
            return null;
        }
    }

//    @Override
//    public List<ArchivesTree> getDataTree(String t, String fondsId, String typeId, String classId) {
//        int index = 0;
//
//        QueryWrapper<ArchivesClass> queryWrapperClass = new QueryWrapper<ArchivesClass>();
//        queryWrapperClass.eq("fondsId", fondsId);
//        queryWrapperClass.eq("typeId", typeId);
//        queryWrapperClass.eq("parentId", classId);
//
//        List<ArchivesClass> classList = archivesClassMapper.selectList(queryWrapperClass);
//        if (classList != null && classList.size() > 0)
//            classList = classList.stream().sorted(Comparator.comparing(ArchivesClass::getSort))
//                    .collect(Collectors.toList());
//        List<ArchivesClass> classTree = getClassTree(classList, fondsId, typeId);
//        if (classTree != null && classTree.size() > 0) {
//            classList.addAll(classTree);
//            List<ArchivesClass> classTree1 = getClassTree(classTree, fondsId, typeId);
//            if (classTree1 != null && classTree1.size() > 0) {
//                classList.addAll(classTree1);
//                List<ArchivesClass> classTree2 = getClassTree(classTree1, fondsId, typeId);
//                if (classTree2 != null && classTree2.size() > 0) {
//                    classList.addAll(classTree2);
//                    List<ArchivesClass> classTree3 = getClassTree(classTree2, fondsId, typeId);
//                    if (classTree3 != null && classTree3.size() > 0)
//                        classList.addAll(classTree3);
//                }
//            }
//        }
//        List<ArchivesTree> resultList = new ArrayList<>();
//        if (classList != null && classList.size() > 0) {
//            //classList.parallelStream().forEach(classListTemp -> {
//
//            for (ArchivesClass classListTemp : classList) {
//                ArchivesClass archivesClass = classListTemp;
//                if (archivesClass.getParentId().intValue() == Integer.parseInt(classId)) {
//                    ArchivesTree treeClass = new ArchivesTree();
//                    if ("1".equals(archivesClass.getClassType())) {
//                        treeClass.setNodeId(archivesClass.getId());
//                        treeClass.setId(fondsId + typeId + classId + archivesClass.getSort() + index);
//                        treeClass.setLabel(archivesClass.getClassName());
//                        treeClass.setNodeType("2");
//                        treeClass.setNodeValue(archivesClass.getClassNum());
//                        treeClass.setModel("1");
//                        treeClass.setIsCatalog(archivesClass.getIsCatalog());
//                        treeClass.setIndex(archivesClass.getSort());
//                        treeClass.setParentId(archivesClass.getParentId());
//                        index++;
//                        resultList.add(treeClass);
//                    } else if ("2".equals(archivesClass.getClassType())) {
//                        if ("archivalYear".equals(archivesClass.getDataColumn())) {
//                            QueryWrapper<ArchivesDoc> queryWrapperYear = new QueryWrapper<ArchivesDoc>();
//                            queryWrapperYear.eq("fondsId", fondsId);
//                            queryWrapperYear.eq("typeId", typeId);
//                            queryWrapperYear.orderByDesc("archivalYear");
//                            queryWrapperYear.groupBy("archivalYear");
//                            queryWrapperYear.select("id", "archivalYear");
//                            List<ArchivesDoc> yearList = archivesDocMapper.selectList(queryWrapperYear);
//                            int indexYear = 1;
//                            for (ArchivesDoc archivesDoc : yearList) {
//                                if (!StringUtils.isEmpty(archivesDoc.getArchivalYear())) {
//                                    ArchivesTree treeDatac = new ArchivesTree();
//                                    treeDatac.setNodeId(archivesClass.getId());
//                                    treeDatac.setId(fondsId + typeId + classId + archivesClass.getSort() + indexYear);
//                                    treeDatac.setLabel(archivesDoc.getArchivalYear());
//                                    treeDatac.setNodeType("2");
//                                    treeDatac.setNodeValue(archivesDoc.getArchivalYear());
//                                    treeDatac.setModel("0");
//                                    treeDatac.setColumn(archivesClass.getDataColumn());
//                                    treeDatac.setIsCatalog(archivesClass.getIsCatalog());
//                                    treeDatac.setIndex(indexYear);
//                                    resultList.add(treeDatac);
//                                    indexYear++;
//                                }
//                            }
//                        } else {
//                            QueryWrapper<Dic> queryWrapperDicc = new QueryWrapper<Dic>();
//                            queryWrapperDicc.eq("dicType", archivesClass.getDataColumn());
//                            queryWrapperDicc.orderByAsc("sort");
//
//                            List<Dic> dicListc = dicMapper.selectList(queryWrapperDicc);
//                            for (Dic dicc : dicListc) {
//                                ArchivesTree treeDatac = new ArchivesTree();
//                                treeDatac.setNodeId(archivesClass.getId());
//                                treeDatac.setId(fondsId + typeId + classId + archivesClass.getSort() + index);
//                                treeDatac.setLabel(dicc.getDicValue());
//                                treeDatac.setNodeType("2");
//                                treeDatac.setNodeValue(dicc.getDicCode());
//                                treeDatac.setModel("0");
//                                treeDatac.setColumn(archivesClass.getDataColumn());
//                                treeDatac.setIsCatalog(archivesClass.getIsCatalog());
//                                treeDatac.setIndex(dicc.getSort());
//                                resultList.add(treeDatac);
//                                index++;
//
//                            }
//                        }
//                    }
//                }
//            }
//            ;
//        }
//        setClassChildren(classList, resultList, Long.parseLong(fondsId), typeId, index);
//
//        List<ArchivesTree> resultDataTree = resultList;
//        if (resultDataTree != null && resultDataTree.size() > 0) {
//            resultDataTree = resultDataTree.stream().sorted(Comparator.comparing(ArchivesTree::getIndex))
//                    .collect(Collectors.toList());
//        }
//
//        return resultDataTree;
//    }

    @Override
    public List<ArchivesTree> getDataTree(String t, String fondsId, String typeId, String classId, UserInfo loginInfo) {
        int index = 0;

        //获取用户数据权限
        Classifys c = userService.buildUserClassifys(loginInfo);
        if (null == c) {
            //获取部门权限
            c = userService.buildUserDeptPermissions(loginInfo);
        }
        List<Long> classIds = c.getClassIds();//类目ids

        List<UserClassify> dataPrivilege = userService.getUserClassify(loginInfo);
        //获取数据权限
        List<String> fieldYearIds = new ArrayList<>();//字段年度ids
        for (UserClassify ucBean : dataPrivilege) {
            if (ucBean.getField()!=null && ucBean.getType() == UserClassifyType.LEVEL_4.getKey() && ucBean.getField().equals("archivalYear"))
                fieldYearIds.add(ucBean.getFieldValue());

        }


        QueryWrapper<ArchivesClass> queryWrapperClass = new QueryWrapper<ArchivesClass>();
        queryWrapperClass.eq("fondsId", fondsId);
        queryWrapperClass.eq("typeId", typeId);
        queryWrapperClass.eq("parentId", classId);
        queryWrapperClass.in(classIds!=null&&classIds.size()>0,"id",classIds);

        List<ArchivesClass> classList = archivesClassMapper.selectList(queryWrapperClass);
        if (classList != null && classList.size() > 0)
            classList = classList.stream().sorted(Comparator.comparing(ArchivesClass::getSort))
                    .collect(Collectors.toList());
        List<ArchivesClass> classTree = getClassTree(classList, fondsId, typeId);
        System.out.println("classTree:"+classTree);
        if (classTree != null && classTree.size() > 0) {
            //改造 lig 2021-07-09
            buildClassList(classList, classTree, fondsId, typeId);
        }
        List<ArchivesTree> resultList = new ArrayList<>();
        if (classList != null && classList.size() > 0) {
            //classList.parallelStream().forEach(classListTemp -> {

            for (ArchivesClass classListTemp : classList) {
                ArchivesClass archivesClass = classListTemp;
                if (archivesClass.getParentId().intValue() == Integer.parseInt(classId)) {
                    ArchivesTree treeClass = new ArchivesTree();
                    if ("1".equals(archivesClass.getClassType())) {
                        treeClass.setNodeId(archivesClass.getId());
                        treeClass.setId(fondsId + typeId + classId + archivesClass.getSort() + index);
                        treeClass.setLabel(archivesClass.getClassName());
                        treeClass.setNodeType("2");
                        treeClass.setNodeValue(archivesClass.getClassNum());
                        treeClass.setModel("1");
                        treeClass.setIsCatalog(archivesClass.getIsCatalog());
                        treeClass.setIndex(archivesClass.getSort());
                        treeClass.setParentId(archivesClass.getParentId());

                        treeClass.setTemplate(archivesClass.getTemplate());
                        treeClass.setClassNum(archivesClass.getTreeStr());
                        index++;
                        resultList.add(treeClass);
                    } else if ("2".equals(archivesClass.getClassType())) {
                        if ("archivalYear".equals(archivesClass.getDataColumn())) {
                            QueryWrapper<ArchivesDoc> queryWrapperYear = new QueryWrapper<ArchivesDoc>();
                            queryWrapperYear.eq("fondsId", fondsId);
                            queryWrapperYear.eq("typeId", typeId);
                            //年度字段增加数据权限  lig  2021-06-28
                            if (BaseUtil.listIsNotNullElement(fieldYearIds))
                                queryWrapperYear.in("archivalYear", fieldYearIds);
                            queryWrapperYear.orderByDesc("archivalYear");
                            queryWrapperYear.groupBy("archivalYear");
                            queryWrapperYear.select("id", "archivalYear");
                            List<ArchivesDoc> yearList = archivesDocMapper.selectList(queryWrapperYear);
                            int indexYear = 1;
                            for (ArchivesDoc archivesDoc : yearList) {
                                if (!StringUtils.isEmpty(archivesDoc.getArchivalYear())) {
                                    ArchivesTree treeDatac = new ArchivesTree();
                                    treeDatac.setNodeId(archivesClass.getId());
                                    treeDatac.setId(fondsId + typeId + classId + archivesClass.getSort() + indexYear);
                                    treeDatac.setLabel(archivesDoc.getArchivalYear());
                                    treeDatac.setNodeType("2");
                                    treeDatac.setNodeValue(archivesDoc.getArchivalYear());
                                    treeDatac.setModel("0");
                                    treeDatac.setColumn(archivesClass.getDataColumn());
                                    treeDatac.setIsCatalog(archivesClass.getIsCatalog());
                                    treeDatac.setTemplate(archivesClass.getTemplate());
                                    treeDatac.setIndex(indexYear);
                                    resultList.add(treeDatac);
                                    indexYear++;
                                }
                            }
                        } else {
                            QueryWrapper<Dic> queryWrapperDicc = new QueryWrapper<Dic>();
                            queryWrapperDicc.eq("dicType", archivesClass.getDataColumn());
                            queryWrapperDicc.orderByAsc("sort");

                            List<Dic> dicListc = dicMapper.selectList(queryWrapperDicc);
                            for (Dic dicc : dicListc) {
                                ArchivesTree treeDatac = new ArchivesTree();
                                treeDatac.setNodeId(archivesClass.getId());
                                treeDatac.setId(fondsId + typeId + classId + archivesClass.getSort() + index);
                                treeDatac.setLabel(dicc.getDicValue());
                                treeDatac.setNodeType("2");
                                treeDatac.setNodeValue(dicc.getDicCode());
                                treeDatac.setModel("0");
                                treeDatac.setColumn(archivesClass.getDataColumn());
                                treeDatac.setIsCatalog(archivesClass.getIsCatalog());
                                treeDatac.setIndex(dicc.getSort());
                                resultList.add(treeDatac);
                                index++;

                            }
                        }
                    }
                }
            }
            ;
        }
        //修改为数据权限方法   lig 2021-06-29
        setClassChildren(classList, resultList, fondsId, typeId, index, fieldYearIds);

        List<ArchivesTree> resultDataTree = resultList;
        if (resultDataTree != null && resultDataTree.size() > 0) {
            resultDataTree = resultDataTree.stream().sorted(Comparator.comparing(ArchivesTree::getIndex))
                    .collect(Collectors.toList());
        }

        return resultDataTree;
    }


    /**
     * @description: 将父类为字段ID的替换为分类ID
     * @author: lig
     * @date: 2021/6/22
     */
    private void buildParentId(ArchivesClass archivesClass) {
        if (archivesClass.getParentId().intValue() > 0) {
            ArchivesClass acBean = archivesClassMapper.selectById(archivesClass.getParentId());
            if (acBean.getClassType().equals("2")) {
                archivesClass.setParentId(acBean.getParentId());
                buildParentId(archivesClass);
            }
        }
    }

    private void buildClassList(List<ArchivesClass> classList, List<ArchivesClass> classTree, String fondsId, String typeId) {
        if (classTree != null && classTree.size() > 0) {
            classList.addAll(classTree);
            List<ArchivesClass> classTree1 = getClassTree(classTree, fondsId, typeId);
            buildClassList(classList, classTree1, fondsId, typeId);
        }
    }


    private ArchivesTree buildArchivesType(ArchivesType archivesType, ArchivesFonds f) {
        ArchivesTree treeNode = new ArchivesTree();
        treeNode.setNodeId(archivesType.getId());
//        treeNode.setId(f.getSort() + "-" + archivesType.getSort());
        treeNode.setId(f.getId().toString() + "-" + archivesType.getId().toString());
        treeNode.setLabel(archivesType.getTypeNum() + archivesType.getTypeName());
        treeNode.setNodeType("1");
        treeNode.setNodeValue(archivesType.getTypeNum());
        treeNode.setModel(archivesType.getModel());
        treeNode.setIsCatalog("1");
        treeNode.setFileType(archivesType.getFileType());
        treeNode.setIndex(archivesType.getSort());
        return treeNode;
    }

    private void buildArchivesClass(ArchivesType archivesType, ArchivesFonds f, List<Long> classIds, List<String> fieldYearIds, ArchivesTree treeNode, List<ArchivesTree> typeTree) {
        List<ArchivesClass> classList = null;

        if (BaseUtil.listIsNotNullElement(classIds)) {
            QueryWrapper<ArchivesClass> classQueryWrapper=new QueryWrapper<>();
            classQueryWrapper.in("id",classIds);
            classQueryWrapper.eq("typeId",archivesType.getId());
            classQueryWrapper.eq("parentId", "0");
            classList = archivesClassMapper.selectList(classQueryWrapper);
        } else {
            QueryWrapper<ArchivesClass> queryWrapperClass = new QueryWrapper<ArchivesClass>();
            queryWrapperClass.eq("fondsId", archivesType.getFondsId());
            queryWrapperClass.eq("typeId", archivesType.getId());
            queryWrapperClass.eq("parentId", "0");
            classList = archivesClassMapper.selectList(queryWrapperClass);
        }

        List<ArchivesTree> resultList = new ArrayList<>();

        if (classList != null && classList.size() > 0) {
            for (ArchivesClass archivesClassTemp : classList) {
                ArchivesClass archivesClass = archivesClassTemp;
                if (archivesClass.getParentId().intValue() == 0) {
                    ArchivesTree treeClass = new ArchivesTree();
                    if ("1".equals(archivesClass.getClassType())) {
                        treeClass.setNodeId(archivesClass.getId());
                        treeClass.setId(f.getId().toString() + "-" + archivesType.getId().toString() + "-" + archivesClass.getId()
                                + "-" + archivesClass.getIsCatalog());
//                        treeClass.setId(f.getSort() + "" + archivesType.getSort() + archivesClass.getIsCatalog()
//                                + archivesClass.getSort());
                        treeClass.setLabel(archivesClass.getClassName());
                        treeClass.setNodeType("2");
                        treeClass.setNodeValue(archivesClass.getClassNum());
                        treeClass.setModel("1");
                        treeClass.setIsCatalog(archivesClass.getIsCatalog());
                        treeClass.setIndex(archivesClass.getSort());
                        treeClass.setParentId(archivesClass.getParentId());
                        treeClass.setTemplate(archivesClass.getTemplate());
                        treeClass.setClassNum(archivesClass.getTreeStr());
                        resultList.add(treeClass);
                    } else if ("2".equals(archivesClass.getClassType())) {
                        if ("archivalYear".equals(archivesClass.getDataColumn())) {
                            QueryWrapper<ArchivesDoc> queryWrapperYear = new QueryWrapper<ArchivesDoc>();
                            queryWrapperYear.eq("fondsId", f.getId());
                            queryWrapperYear.eq("typeId", archivesType.getId());
//									//年度字段增加数据权限  lig  2021-06-28
                            if (BaseUtil.listIsNotNullElement(fieldYearIds))
                                queryWrapperYear.in("archivalYear", fieldYearIds);
                            queryWrapperYear.orderByDesc("archivalYear");
                            queryWrapperYear.groupBy("archivalYear");
                            queryWrapperYear.select("id", "archivalYear");
                            List<ArchivesDoc> yearList = archivesDocMapper.selectList(queryWrapperYear);
                            int indexYear = 1;
                            for (ArchivesDoc archivesDoc : yearList) {
                                if (!StringUtils.isEmpty(archivesDoc.getArchivalYear())) {
                                    ArchivesTree treeDatac = new ArchivesTree();
                                    treeDatac.setNodeId(archivesClass.getId());
                                    treeDatac.setId(f.getId() + "-" + archivesType.getId() + "-" + archivesDoc.getClassId() + "-"
                                            + archivesClass.getIsCatalog() + "-" + archivesDoc.getArchivalYear());
//                                    treeDatac.setId(f.getSort() + "" + archivesType.getSort()
//                                            + archivesClass.getIsCatalog() + archivesDoc.getArchivalYear());
                                    treeDatac.setLabel(archivesDoc.getArchivalYear());
                                    treeDatac.setNodeType("2");
                                    treeDatac.setNodeValue(archivesDoc.getArchivalYear());
                                    treeDatac.setModel("0");
                                    treeDatac.setColumn(archivesClass.getDataColumn());
                                    treeDatac.setIsCatalog(archivesClass.getIsCatalog());
                                    treeDatac.setIndex(indexYear);
                                    resultList.add(treeDatac);
                                    indexYear++;
                                }
                            }
                        } else {
                            QueryWrapper<Dic> queryWrapperDicc = new QueryWrapper<Dic>();
                            queryWrapperDicc.eq("dicType", archivesClass.getDataColumn());
                            queryWrapperDicc.orderByAsc("sort");

                            List<Dic> dicListc = dicMapper.selectList(queryWrapperDicc);
                            for (Dic dicc : dicListc) {
                                ArchivesTree treeDatac = new ArchivesTree();
                                treeDatac.setNodeId(archivesClass.getId());
                                treeDatac.setId(f.getId() + "-" + archivesType.getId() + "-" + archivesClass.getId() + "-"
                                        + archivesClass.getIsCatalog() + "-" + dicc.getSort() + "-" + dicc.getId());
//                                treeDatac.setId(f.getSort() + "" + archivesType.getSort()
//                                        + archivesClass.getIsCatalog() + dicc.getSort() + dicc.getId());
                                treeDatac.setLabel(dicc.getDicValue());
                                treeDatac.setNodeType("2");
                                treeDatac.setNodeValue(dicc.getDicCode());
                                treeDatac.setModel("0");
                                treeDatac.setColumn(archivesClass.getDataColumn());
                                treeDatac.setIsCatalog(archivesClass.getIsCatalog());
                                treeDatac.setIndex(dicc.getSort());
                                resultList.add(treeDatac);
                            }
                        }
                    }
                }
            }
        }
        List<ArchivesTree> resultClassTree = resultList;
        if (resultClassTree != null && resultClassTree.size() > 0) {

            resultClassTree = resultClassTree.stream().sorted(Comparator.comparing(ArchivesTree::getIndex))
                    .collect(Collectors.toList());
        }

        treeNode.setChildren(resultClassTree);
        typeTree.add(treeNode);
    }

    /**
     * 获取全部全宗树,无权限
     * @param fondsId
     * @param model
     * @return
     */
    /*@Override
    public List<ArchivesTree> getFondsTree(String fondsId,String model) {

        //1.获取全部的全宗
        QueryWrapper<ArchivesFonds> fondsQueryWrapper=new QueryWrapper<>();
        fondsQueryWrapper.eq(!StringUtils.isEmpty(fondsId),"id",fondsId);

        List<ArchivesFonds> archivesFondsList = archivesFondsMapper.selectList(fondsQueryWrapper);
        List<ArchivesTree> treesList=new ArrayList<>();
        for(ArchivesFonds fonds:archivesFondsList){
            List<ArchivesTree> archivesTreesFondsList=new ArrayList<>();
            ArchivesTree tree = new ArchivesTree();
            tree.setNodeId(fonds.getId());
            tree.setId(fonds.getId() + "");
            tree.setLabel(fonds.getFondsName());
            tree.setNodeType("0");
            tree.setNodeValue(fonds.getFondsNum());
            tree.setIsCatalog("1");
            tree.setIndex(fonds.getSort());
            //2.根据全宗获取全部门类

            QueryWrapper<ArchivesType> typeQueryWrapper=new QueryWrapper<>();
            typeQueryWrapper.eq("fondsId",fonds.getId());
            typeQueryWrapper.eq(!StringUtils.isEmpty(model),"model", model);
            List<ArchivesType> archivesTypeList = archivesTypeMapper.selectList(typeQueryWrapper);
            int i=1;
            for(ArchivesType type:archivesTypeList){
                List<ArchivesTree> archivesTreesTypeList=new ArrayList<>();
                ArchivesTree typeTree = new ArchivesTree();
                typeTree.setId(fonds.getId()+"-"+type.getId());
                typeTree.setLabel(type.getTypeName());
                typeTree.setNodeValue(i+"");
                typeTree.setNodeId(type.getId());
                typeTree.setModel(type.getModel());
                typeTree.setNodeType("1");
                typeTree.setIsCatalog("1");
                typeTree.setIndex(i);

                //3.根据门类获取对应的分类
                QueryWrapper<ArchivesClass> classQueryWrapper=new QueryWrapper<>();
                classQueryWrapper.eq("typeId",type.getId());
                classQueryWrapper.eq("fondsId",fonds.getId());
                List<ArchivesClass> archivesClassList = archivesClassMapper.selectList(classQueryWrapper);
                //4.对分类形成树形结构
                Map<Long,String> map=new HashMap<>();
                int j=1;
                for(ArchivesClass classParent:archivesClassList){  // 获取分类表假设是父级
                    String nodeType="2";
                    if(classParent.getClassType().equals("2")){
                        nodeType="3";
                    }
                    List<ArchivesTree> archivesTreesList=new ArrayList<>();
                    ArchivesTree classParentTree = new ArchivesTree();
                    classParentTree.setId(fonds.getId()+"-"+type.getId()+"-"+classParent.getId());
                    classParentTree.setLabel(classParent.getClassName());
                    classParentTree.setNodeValue(j+"");
                    classParentTree.setNodeId(classParent.getId());
                    classParentTree.setModel(type.getModel());
                    classParentTree.setParentId(0L);
                    classParentTree.setIsCatalog("1");
                    classParentTree.setNodeType(nodeType);
                    classParentTree.setIndex(j);
                    classParentTree.setTemplate("dataList");
                    //classParentTree.setNodeId()
                    int k=1;
                    for (ArchivesClass classChild:archivesClassList){  // 获取分类表假设是子级
                        if(classChild.getParentId()==classParent.getId()){  //如果子级的parent等于父级id，则将加入父级的子元素中

                            String nodeTypeChild="2";
                            if(classChild.getClassType().equals("2")){
                                nodeTypeChild="3";
                            }

                            ArchivesTree classChildTree = new ArchivesTree();
                            classChildTree.setId(fonds.getId()+"-"+type.getId()+"-"+classParent.getId()+"-"+classChild.getId());
                            classChildTree.setLabel(classChild.getClassName());
                            classChildTree.setNodeValue(k+"");
                            classChildTree.setNodeId(classChild.getId());
                            classChildTree.setModel(type.getModel());
                            classChildTree.setParentId(classParent.getId());
                            classChildTree.setIsCatalog("1");
                            classChildTree.setNodeType(nodeTypeChild);
                            classChildTree.setIndex(k);

                            //判断是否还存在子级
                            QueryWrapper<ArchivesClass> classWrapper=new QueryWrapper<>();
                            classWrapper.eq("parentId",classChild.getId());
                            List<ArchivesClass> classList = archivesClassMapper.selectList(classWrapper);
                            List<ArchivesTree> archivesTreesList_2=new ArrayList<>();
                            for(int f=0;f<classList.size();f++){
                                ArchivesClass archivesClass=classList.get(f);

                                String nodeTypeChild_2="2";
                                if(archivesClass.getClassType().equals("2")){
                                    nodeTypeChild_2="3";
                                }

                                ArchivesTree ChildTree = new ArchivesTree();
                                ChildTree.setId(fonds.getId()+"-"+type.getId()+"-"+classParent.getId()+"-"+classChild.getId()+"-"+archivesClass.getId());
                                ChildTree.setLabel(archivesClass.getClassName());
                                ChildTree.setNodeValue(f+"");
                                ChildTree.setNodeId(archivesClass.getId());
                                ChildTree.setModel(type.getModel());
                                ChildTree.setParentId(classParent.getId());
                                ChildTree.setIsCatalog("1");
                                ChildTree.setNodeType(nodeTypeChild_2);
                                ChildTree.setIndex(f);
                                archivesTreesList_2.add(ChildTree);
                            }
                            classChildTree.setChildren(archivesTreesList_2);
                            archivesTreesList.add(classChildTree);


                            map.put(classChild.getId(),classChild.getClassName());

                            k++;
                        }
                    }
                    classParentTree.setChildren(archivesTreesList);

                    if(map.get(classParent.getId())!=null){
                        continue;
                    }


                    archivesTreesTypeList.add(classParentTree);
                    j++;
                }
                typeTree.setChildren(archivesTreesTypeList);
                archivesTreesFondsList.add(typeTree);

                i++;
            }

            tree.setChildren(archivesTreesFondsList);
            treesList.add(tree);

        }



            //利用双重循环将对应，分类放在父分类下


        //5.将对应分类类存放在对应门类下面
        return treesList;
    }*/

    /**
     * 获取全宗树，有权限
     * @param loginInfo
     * @param model
     * @return
     */
    /*public List<ArchivesTree> getFondsTree(UserInfo loginInfo,String model) {
        //获取用户数据权限
        Classifys c = userService.buildUserClassifys(loginInfo);
        List<Long> afIds = c.getFonds();//全宗ids
        List<Long> categoryIds = c.getCategoryIds();//门类ids
        List<Long> classIds = c.getClassIds();//类目ids
        List<String> fieldYearIds = c.getFieldYearIds();//字段年度ids


        //1.获取全部的全宗
        QueryWrapper<ArchivesFonds> fondsQueryWrapper=new QueryWrapper<>();
        fondsQueryWrapper.in(BaseUtil.listIsNotNullElement(afIds),"id",afIds);

        List<ArchivesFonds> archivesFondsList = archivesFondsMapper.selectList(fondsQueryWrapper);
        List<ArchivesTree> treesList=new ArrayList<>();
        for(ArchivesFonds fonds:archivesFondsList){
            List<ArchivesTree> archivesTreesFondsList=new ArrayList<>();
            ArchivesTree tree = new ArchivesTree();
            tree.setNodeId(fonds.getId());
            tree.setId(fonds.getId() + "");
            tree.setLabel(fonds.getFondsName());
            tree.setNodeType("0");
            tree.setNodeValue(fonds.getFondsNum());
            tree.setIsCatalog("1");
            tree.setIndex(fonds.getSort());

            //2.根据全宗获取全部门类
            QueryWrapper<ArchivesType> typeQueryWrapper=new QueryWrapper<>();
            typeQueryWrapper.eq("fondsId",fonds.getId());
            typeQueryWrapper.eq(!StringUtils.isEmpty(model),"model", model);
            typeQueryWrapper.in(BaseUtil.listIsNotNullElement(categoryIds),"id",categoryIds);
            List<ArchivesType> archivesTypeList = archivesTypeMapper.selectList(typeQueryWrapper);
            int i=1;
            for(ArchivesType type:archivesTypeList){
                List<ArchivesTree> archivesTreesTypeList=new ArrayList<>();
                ArchivesTree typeTree = new ArchivesTree();
                typeTree.setId(fonds.getId()+"-"+type.getId());
                typeTree.setLabel(type.getTypeName());
                typeTree.setNodeValue(i+"");
                typeTree.setNodeId(type.getId());
                typeTree.setModel(type.getModel());
                typeTree.setNodeType("1");
                typeTree.setIsCatalog("1");
                typeTree.setIndex(i);

                //3.根据门类获取对应的分类
                QueryWrapper<ArchivesClass> classQueryWrapper=new QueryWrapper<>();
                classQueryWrapper.eq("typeId",type.getId());
                classQueryWrapper.eq("fondsId",fonds.getId());
                classQueryWrapper.in(BaseUtil.listIsNotNullElement(classIds),"id",classIds);
                List<ArchivesClass> archivesClassList = archivesClassMapper.selectList(classQueryWrapper);
                //4.对分类形成树形结构
                Map<Long,String> map=new HashMap<>();
                int j=1;
                for(ArchivesClass classParent:archivesClassList){  // 获取分类表假设是父级
                    String nodeType="2";
                    if(classParent.getClassType().equals("2")){
                        nodeType="3";
                    }
                    List<ArchivesTree> archivesTreesList=new ArrayList<>();
                    ArchivesTree classParentTree = new ArchivesTree();
                    classParentTree.setId(fonds.getId()+"-"+type.getId()+"-"+classParent.getId());
                    classParentTree.setLabel(classParent.getClassName());
                    classParentTree.setNodeValue(j+"");
                    classParentTree.setNodeId(classParent.getId());
                    classParentTree.setModel(type.getModel());
                    classParentTree.setParentId(0L);
                    classParentTree.setIsCatalog("1");
                    classParentTree.setNodeType(nodeType);
                    classParentTree.setIndex(j);
                    classParentTree.setTemplate("dataList");
                    //classParentTree.setNodeId()
                    int k=1;
                    for (ArchivesClass classChild:archivesClassList){  // 获取分类表假设是子级
                        if(classChild.getParentId()==classParent.getId()){  //如果子级的parent等于父级id，则将加入父级的子元素中

                            String nodeTypeChild="2";
                            if(classChild.getClassType().equals("2")){
                                nodeTypeChild="3";
                            }

                            ArchivesTree classChildTree = new ArchivesTree();
                            classChildTree.setId(fonds.getId()+"-"+type.getId()+"-"+classParent.getId()+"-"+classChild.getId());
                            classChildTree.setLabel(classChild.getClassName());
                            classChildTree.setNodeValue(k+"");
                            classChildTree.setNodeId(classChild.getId());
                            classChildTree.setModel(type.getModel());
                            classChildTree.setParentId(classParent.getId());
                            classChildTree.setIsCatalog("1");
                            classChildTree.setNodeType(nodeTypeChild);
                            classChildTree.setIndex(k);

                            //判断是否还存在子级
                            QueryWrapper<ArchivesClass> classWrapper=new QueryWrapper<>();
                            classWrapper.eq("parentId",classChild.getId());
                            classWrapper.in(BaseUtil.listIsNotNullElement(classIds),"id",classIds);
                            List<ArchivesClass> classList = archivesClassMapper.selectList(classWrapper);
                            List<ArchivesTree> archivesTreesList_2=new ArrayList<>();
                            for(int f=0;f<classList.size();f++){
                                ArchivesClass archivesClass=classList.get(f);

                                String nodeTypeChild_2="2";
                                if(archivesClass.getClassType().equals("2")){
                                    nodeTypeChild_2="3";
                                }

                                ArchivesTree ChildTree = new ArchivesTree();
                                ChildTree.setId(fonds.getId()+"-"+type.getId()+"-"+classParent.getId()+"-"+classChild.getId()+"-"+archivesClass.getId());
                                ChildTree.setLabel(archivesClass.getClassName());
                                ChildTree.setNodeValue(f+"");
                                ChildTree.setNodeId(archivesClass.getId());
                                ChildTree.setModel(type.getModel());
                                ChildTree.setParentId(classParent.getId());
                                ChildTree.setIsCatalog("1");
                                ChildTree.setNodeType(nodeTypeChild_2);
                                ChildTree.setIndex(f);
                                archivesTreesList_2.add(ChildTree);
                            }
                            classChildTree.setChildren(archivesTreesList_2);
                            archivesTreesList.add(classChildTree);


                            map.put(classChild.getId(),classChild.getClassName());

                            k++;
                        }
                    }
                    classParentTree.setChildren(archivesTreesList);

                    if(map.get(classParent.getId())!=null){
                        continue;
                    }


                    archivesTreesTypeList.add(classParentTree);
                    j++;
                }
                typeTree.setChildren(archivesTreesTypeList);
                archivesTreesFondsList.add(typeTree);

                i++;
            }

            tree.setChildren(archivesTreesFondsList);
            treesList.add(tree);

        }



        //利用双重循环将对应，分类放在父分类下


        //5.将对应分类类存放在对应门类下面
        return treesList;
    }*/

    /**
     * 获取全部全宗树，升级版  无权限
     * @param fondsId
     * @param model
     * @return
     */
    public List<ArchivesTree> getFondsTreeUpgrade(String fondsId,String model){
        //1.获取全部的全宗
        QueryWrapper<ArchivesFonds> fondsQueryWrapper=new QueryWrapper<>();

        fondsQueryWrapper.eq(!StringUtils.isEmpty(fondsId),"id",fondsId);

        List<ArchivesFonds> archivesFondsList = archivesFondsMapper.selectList(fondsQueryWrapper);
        List<ArchivesTree> treesList=new ArrayList<>();
        for(ArchivesFonds fonds:archivesFondsList) {
            List<ArchivesTree> archivesTreesFondsList = new ArrayList<>();
            ArchivesTree tree = new ArchivesTree();
            tree.setNodeId(fonds.getId());
            tree.setId(fonds.getId() + "");
            tree.setLabel(fonds.getFondsName());
            tree.setNodeType("0");
            tree.setNodeValue(fonds.getFondsNum());
            tree.setIsCatalog("1");
            tree.setIndex(fonds.getSort());
            //2.根据全宗获取全部门类
            QueryWrapper<ArchivesType> typeQueryWrapper = new QueryWrapper<>();
            typeQueryWrapper.eq("fondsId", fonds.getId());
            typeQueryWrapper.eq(!StringUtils.isEmpty(model), "model", model);
            List<ArchivesType> archivesTypeList = archivesTypeMapper.selectList(typeQueryWrapper);
            int i=1;
            for(ArchivesType type:archivesTypeList) {
                List<ArchivesTree> archivesTreesTypeList = new ArrayList<>();
                ArchivesTree typeTree = new ArchivesTree();
                typeTree.setId(fonds.getId() + "-" + type.getId());
                typeTree.setLabel(type.getTypeName());
                typeTree.setNodeValue(i + "");
                typeTree.setNodeId(type.getId());
                typeTree.setModel(type.getModel());
                typeTree.setNodeType("1");
                typeTree.setIsCatalog("1");
                typeTree.setIndex(i);

                //3.根据门类获取对应的分类
                QueryWrapper<ArchivesClass> classQueryWrapper = new QueryWrapper<>();
                classQueryWrapper.eq("typeId", type.getId());
                classQueryWrapper.eq("fondsId", fonds.getId());
                classQueryWrapper.eq("parentId", "0");
                List<ArchivesClass> archivesClassList = archivesClassMapper.selectList(classQueryWrapper);
                int j=1;
                for(ArchivesClass archivesClass:archivesClassList){
                    ArchivesTree classTree = new ArchivesTree();
                    String treeId=fonds.getId() + "-" + type.getId() + "-" + archivesClass.getId();
                    classTree.setId(treeId);
                    classTree.setLabel(archivesClass.getClassName());
                    classTree.setNodeValue(j + "");
                    classTree.setNodeId(archivesClass.getId());
                    classTree.setModel(type.getModel());
                    classTree.setNodeType("2");
                    classTree.setIsCatalog("1");
                    classTree.setParentId(0L);
                    classTree.setIsCatalog(archivesClass.getIsCatalog());
                    classTree.setTemplate(archivesClass.getTemplate());
                    classTree.setIndex(j);

                    int nodeType=2;
                    List<ArchivesTree> classChildTree=getClassChild(type.getId(),fonds.getId(),archivesClass.getId(),type.getModel(),treeId,nodeType,null);
                    classTree.setChildren(classChildTree);
                    archivesTreesTypeList.add(classTree);
                    j++;
                }


                typeTree.setChildren(archivesTreesTypeList);
                archivesTreesFondsList.add(typeTree);
                i++;
            }

        tree.setChildren(archivesTreesFondsList);
        treesList.add(tree);

        }
        return treesList;
    }


    /**
     * 获取全部全宗树，升级版  有权限
     * @param loginInfo
     * @param model
     * @return
     */
    public List<ArchivesTree> getFondsTreeUpgrade(UserInfo loginInfo,String model){

        //获取用户数据权限
        Classifys c = userService.buildUserClassifys(loginInfo);
        List<Long> afIds = c.getFonds();//全宗ids
        List<Long> categoryIds = c.getCategoryIds();//门类ids
        List<Long> classIds = c.getClassIds();//类目ids
        List<String> fieldYearIds = c.getFieldYearIds();//字段年度ids

        //1.获取全部的全宗
        QueryWrapper<ArchivesFonds> fondsQueryWrapper=new QueryWrapper<>();
        fondsQueryWrapper.in(BaseUtil.listIsNotNullElement(afIds),"id",afIds);

        List<ArchivesFonds> archivesFondsList = archivesFondsMapper.selectList(fondsQueryWrapper);
        List<ArchivesTree> treesList=new ArrayList<>();
        for(ArchivesFonds fonds:archivesFondsList) {
            List<ArchivesTree> archivesTreesFondsList = new ArrayList<>();
            ArchivesTree tree = new ArchivesTree();
            tree.setNodeId(fonds.getId());
            tree.setId(fonds.getId() + "");
            tree.setLabel(fonds.getFondsName());
            tree.setNodeType("0");
            tree.setNodeValue(fonds.getFondsNum());
            tree.setIsCatalog("1");
            tree.setIndex(fonds.getSort());
            //2.根据全宗获取全部门类
            QueryWrapper<ArchivesType> typeQueryWrapper = new QueryWrapper<>();
            typeQueryWrapper.eq("fondsId", fonds.getId());
            typeQueryWrapper.eq(!StringUtils.isEmpty(model), "model", model);
            typeQueryWrapper.in(BaseUtil.listIsNotNullElement(categoryIds),"id",categoryIds);
            List<ArchivesType> archivesTypeList = archivesTypeMapper.selectList(typeQueryWrapper);
            int i=1;
            for(ArchivesType type:archivesTypeList) {
                List<ArchivesTree> archivesTreesTypeList = new ArrayList<>();
                ArchivesTree typeTree = new ArchivesTree();
                typeTree.setId(fonds.getId() + "-" + type.getId());
                typeTree.setLabel(type.getTypeName());
                typeTree.setNodeValue(i + "");
                typeTree.setNodeId(type.getId());
                typeTree.setModel(type.getModel());
                typeTree.setNodeType("1");
                typeTree.setIsCatalog("1");
                typeTree.setIndex(i);

                //3.根据门类获取对应的分类
                QueryWrapper<ArchivesClass> classQueryWrapper = new QueryWrapper<>();
                classQueryWrapper.eq("typeId", type.getId());
                classQueryWrapper.eq("fondsId", fonds.getId());
                classQueryWrapper.eq("parentId", "0");
                classQueryWrapper.in(BaseUtil.listIsNotNullElement(classIds),"id",classIds);
                List<ArchivesClass> archivesClassList = archivesClassMapper.selectList(classQueryWrapper);
                int j=1;
                for(ArchivesClass archivesClass:archivesClassList){
                    ArchivesTree classTree = new ArchivesTree();
                    String treeId=fonds.getId() + "-" + type.getId() + "-" + archivesClass.getId();
                    classTree.setId(treeId);
                    classTree.setLabel(archivesClass.getClassName());
                    classTree.setNodeValue(j + "");
                    classTree.setNodeId(archivesClass.getId());
                    classTree.setModel(type.getModel());
                    classTree.setNodeType("2");
                    classTree.setIsCatalog("1");
                    classTree.setParentId(0L);
                    classTree.setIsCatalog(archivesClass.getIsCatalog());
                    classTree.setTemplate(archivesClass.getTemplate());
                    classTree.setIndex(j);

                    int nodeType=2;
                    List<ArchivesTree> classChildTree=getClassChild(type.getId(),fonds.getId(),archivesClass.getId(),type.getModel(),treeId,nodeType,classIds);
                    classTree.setChildren(classChildTree);
                    archivesTreesTypeList.add(classTree);
                    j++;
                }


                typeTree.setChildren(archivesTreesTypeList);
                archivesTreesFondsList.add(typeTree);
                i++;
            }

            tree.setChildren(archivesTreesFondsList);
            treesList.add(tree);

        }
        return treesList;
    }

    private List<ArchivesTree> getClassChild(long typeId,long fondsId,long parentId,String model,String treeId,int nodeType,List<Long> classIds){
        QueryWrapper<ArchivesClass> classQueryWrapper = new QueryWrapper<>();
        classQueryWrapper.eq("typeId",typeId);
        classQueryWrapper.eq("fondsId", fondsId);
        classQueryWrapper.eq("parentId", parentId);
        classQueryWrapper.in(BaseUtil.listIsNotNullElement(classIds),"id",classIds);
        List<ArchivesClass> archivesClassList = archivesClassMapper.selectList(classQueryWrapper);
        List<ArchivesTree> treesList=new ArrayList<>();
        nodeType++;
        int i=1;
        if(archivesClassList.size()>0){
            for(ArchivesClass archivesClass:archivesClassList){
                ArchivesTree classTree = new ArchivesTree();
                classTree.setId(treeId + "-" + archivesClass.getId());
                classTree.setLabel(archivesClass.getClassName());
                classTree.setNodeValue(i + "");
                classTree.setNodeId(archivesClass.getId());
                classTree.setModel(model);
                classTree.setNodeType(nodeType+"");
                classTree.setIsCatalog(archivesClass.getIsCatalog());
                classTree.setTemplate(archivesClass.getTemplate());
                classTree.setParentId(parentId);
                classTree.setIndex(i);
                List<ArchivesTree> listArchivesTree= getClassChild( typeId, fondsId,archivesClass.getId(), model,treeId,nodeType,classIds);
                i++;
                classTree.setChildren(listArchivesTree);
                treesList.add(classTree);

            }
            return treesList;
        }else{
            return treesList;
        }


    }




    /*public static void main(String[] args) {
        System.out.println("22/1".contains("//"));
    }
*/
}
